﻿using System;
using System.Linq;
using Microsoft.SharePoint;
using Repo.Interfaces;

namespace Repo.Converters
{
    public static class ContentTypeFinder
    {
        public static SPContentType GetContentTypeFromType(Type type)
        {
            var contentType =
                SPContext.Current.Site.RootWeb.ContentTypes.Cast<SPContentType>().FirstOrDefault(
                    ct => ct.Name == GetContentTypeNameFromT(type));
            return contentType;
        }

        public static string GetContentTypeNameFromT(Type type)
        {
            var pocoInstance = Activator.CreateInstance(type);
            return ((ISharePointItem)pocoInstance).GetAssociatedContentTypeName();
        }
    }
}
